package com.hilaver.dzmis.web.servlet.stock;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryButton;
import com.hilaver.dzmis.basicinfo.BiAccessoryButtonInout;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthersInout;
import com.hilaver.dzmis.basicinfo.BiAccessoryZip;
import com.hilaver.dzmis.basicinfo.BiAccessoryZipInout;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.basicinfo.BiDZColorInout;
import com.hilaver.dzmis.service.impl.StockServiceImpl;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class StockServlet extends AbstractBaseServlet{
	
	public final static int ACTION_YARN_STOCK = 10;
	public final static int ACTION_EDIT_YARN_STOCK_INOUT = 11;
	public final static int ACTION_GET_ALL_YARN_STOCK_INOUT = 12;
	public final static int ACTION_DELETE_YARN_STOCK_INOUT = 13;
	public final static int ACTION_GET_YARN_STOCK_INOUT = 14;
	
	public final static int ACTION_ACCESSORY_BUTTON_STOCK = 20;
	public final static int ACTION_EDIT_ACCESSORY_BUTTON_STOCK_INOUT = 21;
	public final static int ACTION_GET_ALL_ACCESSORY_BUTTON_STOCK_INOUT = 22;
	public final static int ACTION_DELETE_ACCESSORY_BUTTON_STOCK_INOUT = 23;
	public final static int ACTION_GET_ACCESSORY_BUTTON_STOCK_INOUT = 24;
	
	public final static int ACTION_ACCESSORY_ZIP_STOCK = 30;
	public final static int ACTION_EDIT_ACCESSORY_ZIP_STOCK_INOUT = 31;
	public final static int ACTION_GET_ALL_ACCESSORY_ZIP_STOCK_INOUT = 32;
	public final static int ACTION_DELETE_ACCESSORY_ZIP_STOCK_INOUT = 33;
	public final static int ACTION_GET_ACCESSORY_ZIP_STOCK_INOUT = 34;
	
	public final static int ACTION_ACCESSORY_OTHERS_STOCK = 40;
	public final static int ACTION_EDIT_ACCESSORY_OTHERS_STOCK_INOUT = 41;
	public final static int ACTION_GET_ALL_ACCESSORY_OTHERS_STOCK_INOUT = 42;
	public final static int ACTION_DELETE_ACCESSORY_OTHERS_STOCK_INOUT = 43;
	public final static int ACTION_GET_ACCESSORY_OTHERS_STOCK_INOUT = 44;
	
	private static final long serialVersionUID = 1L;

	public StockServlet(){
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT_YARN_STOCK_INOUT || this.action == ACTION_EDIT_ACCESSORY_BUTTON_STOCK_INOUT
					|| this.action == ACTION_EDIT_ACCESSORY_ZIP_STOCK_INOUT || this.action == ACTION_EDIT_ACCESSORY_OTHERS_STOCK_INOUT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		StockServiceImpl stockService = new StockServiceImpl();
		stockService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		stockService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		String rtn = "";
		
		List<String> filterList;
		if(this.filters == null){
			filterList = new ArrayList<String>();
		}else{
			filterList = Arrays.asList(this.filters);
		}
		
		Integer dzColorId;
		Integer accessoryButtonId;
		Integer accessoryZipId;
		Integer accessoryOthersId;
		
		this.setPaginationParam(request);
		this.setFilterParam(request);
		switch (this.action) {
			case ACTION_YARN_STOCK:
				rtn = stockService.getDZColorStock(this.page, this.sort, this.dir, this.filters);
			break;
			case ACTION_EDIT_YARN_STOCK_INOUT:
				super.fillObj(BiDZColorInout.class.getName(), request);
				dzColorId = Integer.parseInt((String)this.getValue(request, "dzColorId"));
				BiDZColor color = new BiDZColor();
				color.setId(dzColorId);
				((BiDZColorInout)this.obj).setBiDZColor(color);
				rtn = stockService.edit(this.obj);
			break;
			case ACTION_GET_ALL_YARN_STOCK_INOUT:
				super.setId(request);
				filterList.add("biDZColor.id=" + this.id);
				rtn = stockService.getAll(BiDZColorInout.class.getName(), this.sort, this.dir, (String[])filterList.toArray(new String[0]));
			break;
			case ACTION_DELETE_YARN_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.delete(BiDZColorInout.class.getName(), this.id);
			break;
			case ACTION_GET_YARN_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.get(BiDZColorInout.class.getName(), this.id);
			break;
			
			case ACTION_ACCESSORY_BUTTON_STOCK:
				rtn = stockService.getButtonStock(this.page, this.sort, this.dir, this.filters);
			break;
			case ACTION_EDIT_ACCESSORY_BUTTON_STOCK_INOUT:
				super.fillObj(BiAccessoryButtonInout.class.getName(), request);
				accessoryButtonId = Integer.parseInt((String)this.getValue(request, "accessoryButtonId"));
				BiAccessoryButton biAccessoryButton = new BiAccessoryButton();
				biAccessoryButton.setId(accessoryButtonId);
				((BiAccessoryButtonInout)this.obj).setBiAccessoryButton(biAccessoryButton);
				rtn = stockService.edit(this.obj);
			break;
			case ACTION_GET_ALL_ACCESSORY_BUTTON_STOCK_INOUT:
				super.setId(request);
				filterList.add("biAccessoryButton.id=" + this.id);
				rtn = stockService.getAll(BiAccessoryButtonInout.class.getName(), this.sort, this.dir, (String[])filterList.toArray(new String[0]));
			break;
			case ACTION_DELETE_ACCESSORY_BUTTON_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.delete(BiAccessoryButtonInout.class.getName(), this.id);
			break;
			case ACTION_GET_ACCESSORY_BUTTON_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.get(BiAccessoryButtonInout.class.getName(),this.id);
			break;
			
			case ACTION_ACCESSORY_ZIP_STOCK:
				rtn = stockService.getZipStock(this.page, this.sort, this.dir, this.filters);
			break;
			case ACTION_EDIT_ACCESSORY_ZIP_STOCK_INOUT:
				super.fillObj(BiAccessoryZipInout.class.getName(), request);
				accessoryZipId = Integer.parseInt((String)this.getValue(request, "accessoryZipId"));
				BiAccessoryZip biAccessoryZip = new BiAccessoryZip();
				biAccessoryZip.setId(accessoryZipId);
				((BiAccessoryZipInout)this.obj).setBiAccessoryZip(biAccessoryZip);
				rtn = stockService.edit(this.obj);
				break;
			case ACTION_GET_ALL_ACCESSORY_ZIP_STOCK_INOUT:
				super.setId(request);
				filterList.add("biAccessoryZip.id=" + this.id);
				rtn = stockService.getAll(BiAccessoryZipInout.class.getName(), this.sort, this.dir, (String[])filterList.toArray(new String[0]));
				break;
			case ACTION_DELETE_ACCESSORY_ZIP_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.delete(BiAccessoryZipInout.class.getName(), this.id);
				break;
			case ACTION_GET_ACCESSORY_ZIP_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.get(BiAccessoryZipInout.class.getName(), this.id);
				break;
			
			case ACTION_ACCESSORY_OTHERS_STOCK:
				rtn = stockService.getOthersStock(this.page, this.sort, this.dir, this.filters);
			break;
			case ACTION_EDIT_ACCESSORY_OTHERS_STOCK_INOUT:
				super.fillObj(BiAccessoryOthersInout.class.getName(), request);
				accessoryOthersId = Integer.parseInt((String)this.getValue(request, "accessoryOthersId"));
				BiAccessoryOthers biAccessoryOthers = new BiAccessoryOthers();
				biAccessoryOthers.setId(accessoryOthersId);
				((BiAccessoryOthersInout)this.obj).setBiAccessoryOthers(biAccessoryOthers);
				rtn = stockService.edit(this.obj);
			break;
			case ACTION_GET_ALL_ACCESSORY_OTHERS_STOCK_INOUT:
				super.setId(request);
				filterList.add("biAccessoryOthers.id=" + this.id);
				rtn = stockService.getAll(BiAccessoryOthersInout.class.getName(), this.sort, this.dir, (String[])filterList.toArray(new String[0]));
				break;
			case ACTION_DELETE_ACCESSORY_OTHERS_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.delete(BiAccessoryOthersInout.class.getName(), this.id);
				break;
			case ACTION_GET_ACCESSORY_OTHERS_STOCK_INOUT:
				super.setId(request);
				rtn = stockService.get(BiAccessoryOthersInout.class.getName(), this.id);
				break;
		}
		
		//System.out.println(rtn);
		return rtn;
	}
}
